package com.yxuan.springboot.util;

import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.exception.CosClientException;
import com.qcloud.cos.model.PutObjectRequest;
import com.qcloud.cos.model.PutObjectResult;
import com.qcloud.cos.region.Region;
import org.springframework.stereotype.Component;

import java.io.File;


@Component
public class COSImage {

    // 1 初始化用户身份信息（secretId, secretKey）。
// SECRETID 和 SECRETKEY 请登录访问管理控制台 https://console.cloud.tencent.com/cam/capi 进行查看和管理
    private String secretId = "AKIDqUs1tVt1xd3FAu98qITO4XosnTtkRuSz";//用户的 SecretId，建议使用子账号密钥，授权遵循最小权限指引，降低使用风险。子账号密钥获取可参见 https://cloud.tencent.com/document/product/598/37140
    private String secretKey = "QbYO5XkiU2OWR9brskV2KQHrLgroX1GG";//用户的 SecretKey，建议使用子账号密钥，授权遵循最小权限指引，降低使用风险。子账号密钥获取可参见 https://cloud.tencent.com/document/product/598/37140


//    private String secretId ="AKIDqUs1tVt1xd3FAu98qITO4XosnTtkRuSz";
//    private String secretKey = "QbYO5XkiU2OWR9brskV2KQHrLgroX1GG";


    private COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);
    // 2 设置 bucket 的地域, COS 地域的简称请参见 https://cloud.tencent.com/document/product/436/6224
// clientConfig 中包含了设置 region, https(默认 http), 超时, 代理等 set 方法, 使用可参见源码或者常见问题 Java SDK 部分。
    private Region region = new Region("ap-nanjing");
    private ClientConfig clientConfig = new ClientConfig(region);
    // 3 生成 cos 客户端。
    private COSClient cosClient = new COSClient(cred, clientConfig);

//上传头像
    public PutObjectResult onLoad_img(String filename,String localFilePath,String key){
        // 指定要上传的文件,本地地址

        File localFile = new File(localFilePath);
        // 指定文件将要存放的存储桶
        String bucketName = "volunteer-1318337852";
//                String bucketName = "zyhh-1318337852";

        // 指定文件上传到 COS 上的路径，即对象键。例如对象键为 folder/picture.jpg，则表示将文件 picture.jpg 上传到 folder 路径下


//        String key = "school_logo/"+filename;
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, localFile);
        PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);
        return putObjectResult;
    }


    public String shutDown(){
        cosClient.shutdown();
        return "shutDown";
    }

    public boolean deleteFile(String key) {


        COSClient cosclient = null;
        // 指定文件将要存放的存储桶
        String bucketName = "volunteer-1318337852";
        try {
            //初始化用户身份信息(secretId,secretKey)
            COSCredentials cosCredentials = new BasicCOSCredentials(secretId, secretKey);
            //设置bucket的区域
            ClientConfig clientConfig = new ClientConfig(region);
            // 生成cos客户端
            cosclient = new COSClient(cosCredentials, clientConfig);
            // 指定要删除的 bucket 和路径
            cosclient.deleteObject(bucketName, key);
            // 关闭客户端(关闭后台线程)
            cosclient.shutdown();
        }catch (CosClientException e) {
//            e.printStackTrace();
            return false;
        }
        return true;
    }


}
